package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seawaterbt.ssm.config.DataSource;
import com.seawaterbt.ssm.config.DataSourceEnum;
import com.seawaterbt.ssm.entity.CzBasCrew;
import com.seawaterbt.ssm.entity.CzDatCrewData;
import com.seawaterbt.ssm.entity.ShengDatCrewData;
import com.seawaterbt.ssm.mapper.ShengDatCrewDataMapper;
import com.seawaterbt.ssm.service.CzBasCrewService;
import com.seawaterbt.ssm.service.CzDatCrewdataService;
import com.seawaterbt.ssm.service.ShengDatCrewDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhf
 * @since 2019-10-31
 */
@Service
public class ShengDatCrewDataServiceImpl extends ServiceImpl<ShengDatCrewDataMapper, ShengDatCrewData> implements ShengDatCrewDataService {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Autowired
    CzDatCrewdataService datCrewdataService;
    @Autowired
    CzBasCrewService basCrewService;

    @DataSource(DataSourceEnum.DB1)
    @Override
    public ShengDatCrewData getByCodeTime(QueryWrapper wrapper) {
        return this.getOne(wrapper);
    }
    @Override
    public void sendData() {
        QueryWrapper<CzDatCrewData> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("crew_code", 130981011).or().likeRight("crew_code", 130903007);

        List<CzDatCrewData> czDatCrewData = datCrewdataService.list(queryWrapper);
        Date date = new Date();
        for (CzDatCrewData czDatCrew : czDatCrewData) {
            CzBasCrew one = basCrewService.listOne(czDatCrew.getCrewCode());
          
            if (one != null && one.getCodeProvince() != null && !one.getCodeProvince().equals("")) {

                QueryWrapper wrapper = new QueryWrapper();
                wrapper.eq("CrewID", one.getCodeProvince());
                wrapper.eq("GetTime", czDatCrew.getGetTime());
                ShengDatCrewData byCodeTime = getByCodeTime(wrapper);
                if (byCodeTime == null) {
                    ShengDatCrewData datCrewData = new ShengDatCrewData();
                    datCrewData.setCrewId(one.getCodeProvince());
                    datCrewData.setAddTime(date);
                    datCrewData.setGetTime(czDatCrew.getGetTime());
                    datCrewData.setBackPre(Double.valueOf(czDatCrew.getBackPre()));
                    datCrewData.setBackTemp(Double.valueOf(czDatCrew.getBackTemp()));
                    datCrewData.setCumulativeHeat(Double.valueOf(czDatCrew.getCumulativeHeat()));
                    datCrewData.setInstantaneousFlow(Double.valueOf(czDatCrew.getInstantaneousFlow()));
                    datCrewData.setSupplyPre(Double.valueOf(czDatCrew.getSupplyPre()));
                    datCrewData.setSupplyTemp(Double.valueOf(czDatCrew.getSupplyTemp()));
                    this.save(datCrewData);

                }
            }

        }

        System.out.println("完成同步机组到中间库 " + df.format(new Date()));

    }

    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean saveOrUpdate(ShengDatCrewData entity) {
        return super.saveOrUpdate(entity);
    }
    @DataSource(DataSourceEnum.DB1)
    @Override
    public boolean save(ShengDatCrewData entity) {
        return super.save(entity);
    }
}
